package di.ass.edit;

import di.ass.database.DatabaseHandler;
import di.ass.database.Food;
import di.ass.database.Formula;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import di.ass.R;

public class AlertsEdit {
	private static int formula_id;
	private static int food_id;

	public static void ShowFormulaEdittedAlert(Context con) {
		DatabaseHandler db = new DatabaseHandler(con);

		String[] textEdit = null;
		if (db.getLanguageUse().equals("Việt Nam"))
			textEdit = con.getResources()
					.getStringArray(R.array.edit_dialog_vi);
		else if (db.getLanguageUse().equals("English")) {
			textEdit = con.getResources().getStringArray(
					R.array.edit_dialog_eng);
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(con);
		builder.setTitle(textEdit[0]);
		builder.setIcon(R.drawable.info_success);
		builder.setMessage(textEdit[1]);
		builder.setPositiveButton("OK", null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public static void ShowFormulaNotEditAlert(Context con) {
		DatabaseHandler db = new DatabaseHandler(con);

		String[] textEdit = null;
		if (db.getLanguageUse().equals("Việt Nam"))
			textEdit = con.getResources()
					.getStringArray(R.array.edit_dialog_vi);
		else if (db.getLanguageUse().equals("English")) {
			textEdit = con.getResources().getStringArray(
					R.array.edit_dialog_eng);
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(con);
		builder.setTitle(textEdit[0]);
		builder.setIcon(R.drawable.info_fail);
		builder.setMessage(textEdit[3]);
		builder.setPositiveButton("OK", null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public static void ShowFoodEdittedAlert(Context con) {
		DatabaseHandler db = new DatabaseHandler(con);

		String[] textEdit = null;
		if (db.getLanguageUse().equals("Việt Nam"))
			textEdit = con.getResources()
					.getStringArray(R.array.edit_dialog_vi);
		else if (db.getLanguageUse().equals("English")) {
			textEdit = con.getResources().getStringArray(
					R.array.edit_dialog_eng);
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(con);
		builder.setTitle(textEdit[0]);
		builder.setIcon(R.drawable.info_success);
		builder.setMessage(textEdit[2]);
		builder.setPositiveButton("OK", null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public static void ShowFoodNotEditAlert(Context con) {
		DatabaseHandler db = new DatabaseHandler(con);

		String[] textEdit = null;
		if (db.getLanguageUse().equals("Việt Nam"))
			textEdit = con.getResources()
					.getStringArray(R.array.edit_dialog_vi);
		else if (db.getLanguageUse().equals("English")) {
			textEdit = con.getResources().getStringArray(
					R.array.edit_dialog_eng);
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(con);
		builder.setTitle(textEdit[0]);
		builder.setIcon(R.drawable.info_fail);
		builder.setMessage(textEdit[4]);
		builder.setPositiveButton("OK", null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public static AlertDialog ShowEditFormulaDialog(final Context context,
			final Formula oldFormula) {
		final DatabaseHandler db = new DatabaseHandler(context);

		String[] textEdit = null;
		String[] textF = null;
		if (db.getLanguageUse().equals("Việt Nam")) {
			textEdit = context.getResources().getStringArray(
					R.array.edit_dialog_vi);
			textF = context.getResources().getStringArray(R.array.textview_vi);
		} else if (db.getLanguageUse().equals("English")) {
			textEdit = context.getResources().getStringArray(
					R.array.edit_dialog_eng);
			textF = context.getResources().getStringArray(R.array.textview_eng);
		}

		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(textEdit[5]);
		LayoutInflater layoutInflater = LayoutInflater.from(context);
		View view = layoutInflater.inflate(R.layout.input_formula, null);
		builder.setIcon(R.drawable.info);
		builder.setView(view);
		TextView textViewNameF = (TextView) view
				.findViewById(R.id.textViewNameF);
		textViewNameF.setText(textF[0]);
		TextView textViewData = (TextView) view
				.findViewById(R.id.textViewDateF);
		textViewData.setText(textF[1]);

		final TextView txtName = (TextView) view.findViewById(R.id.nameF);
		final DatePicker datePicker = (DatePicker) view
				.findViewById(R.id.datePicker1);
		formula_id = oldFormula.getId();
		txtName.setText(oldFormula.getName());
		String date = oldFormula.getDate();
		String[] temp;
		temp = date.split("/");
		datePicker.updateDate(Integer.valueOf(temp[0]),
				Integer.valueOf(temp[1]) - 1, Integer.valueOf(temp[2]));

		txtName.setHint(textEdit[6] + " " + (db.getMaxIdFormula() + 1));

		builder.setPositiveButton(textEdit[7], new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				// TODO Auto-generated method stub
				String name = null;
				if (txtName.getText().toString().equals("") == false) {
					name = txtName.getText().toString();
				} else {
					name = txtName.getHint().toString();
				}
				Formula formula = new Formula();
				formula.setId(formula_id);
				formula.setName(name);
				formula.setDate(datePicker.getYear() + "/"
						+ (datePicker.getMonth() + 1) + "/"
						+ datePicker.getDayOfMonth());
				if (db.updateFormula(formula))
					ShowFormulaEdittedAlert(context);
				else {
					ShowFormulaNotEditAlert(context);
				}

			}
		});
		builder.setNegativeButton(textEdit[8], null);
		return builder.create();
	}

	public static AlertDialog ShowEditFoodDialog(final Context context,
			final Food oldFood) {
		DatabaseHandler db = new DatabaseHandler(context);

		String[] textEdit = null;
		String[] textFood = null;
		if (db.getLanguageUse().equals("Việt Nam")) {
			textEdit = context.getResources().getStringArray(
					R.array.edit_dialog_vi);
			textFood = context.getResources().getStringArray(
					R.array.textview_vi);
		} else if (db.getLanguageUse().equals("English")) {
			textEdit = context.getResources().getStringArray(
					R.array.edit_dialog_eng);
			textFood = context.getResources().getStringArray(
					R.array.textview_eng);
		}

		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(textEdit[9]);
		LayoutInflater layoutInflater = LayoutInflater.from(context);
		View view = layoutInflater.inflate(R.layout.input_food, null);
		builder.setIcon(R.drawable.info);
		builder.setView(view);
		TextView textViewNameF = (TextView) view
				.findViewById(R.id.textViewNameFood);
		textViewNameF.setText(textFood[2]);
		TextView textViewData = (TextView) view
				.findViewById(R.id.textViewQuanFood);
		textViewData.setText(textFood[3]);

		final TextView txtFoodName = (TextView) view
				.findViewById(R.id.editNameFood);
		final TextView txtFoodQuantity = (TextView) view
				.findViewById(R.id.editQuantity);
		final TextView txtFoodUnitary = (TextView) view
				.findViewById(R.id.editUnitary);

		food_id = oldFood.getId();
		Food food = db.getOneFood(food_id);
		txtFoodName.setText(food.getName());
		txtFoodQuantity.setText(food.getQuantity());
		txtFoodUnitary.setText(food.getUnitary());

		builder.setPositiveButton(textEdit[7], new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				if (txtFoodName.getText().toString().equals("") == false) {
					Food food = new Food();
					food.setId(food_id);
					food.setName(txtFoodName.getText().toString());
					food.setId_formula(oldFood.getId_formula());
					food.setQuantity(txtFoodQuantity.getText().toString());
					food.setUnitary(txtFoodUnitary.getText().toString());

					DatabaseHandler db = new DatabaseHandler(context);
					if (db.updateFood(food))
						ShowFoodEdittedAlert(context);
					else {
						ShowFoodNotEditAlert(context);
					}
				} else {
					ShowFoodNotEditAlert(context);
				}

			}
		});
		builder.setNegativeButton(textEdit[8], null);
		return builder.create();

	}
}
